package com.example.reader.sdk;

import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;
import android.util.Log;

import com.example.mt3yreader.mt3yApi;
import com.example.reader.sdk.bean.ReaderBean;
import com.example.reader.sdk.receiver.ReaderReicever;

import java.io.IOException;

public class DeviceReaderA extends CardReader {
    public static final String ACTION_USB_PERMISSION = "com.android.example.USB_PERMISSION";
    private static final String TAG = "DeviceReaderA";
    private Context mContext;
    public UsbManager mUsbManager;
    private ReaderBean mReaderBean;
    private ReaderReicever mReaderReicever;

    public DeviceReaderA(Context context) {
        this.mContext = context;
        this.mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
        this.mReaderBean = new ReaderBean();

    }

    @Override
    public void init(IReaderCallback readerCallback) {
        if (mReaderReicever == null) {
            mReaderReicever = new ReaderReicever(readerCallback, mUsbManager, mReaderBean);
        }
        getAuthorityForDevice(readerCallback);
    }


    private void getAuthorityForDevice(IReaderCallback readerCallback) {
        PendingIntent pendingIntent = PendingIntent.getBroadcast(mContext, 0, new Intent(ACTION_USB_PERMISSION), 0);
        mContext.registerReceiver(mReaderReicever, new IntentFilter(ACTION_USB_PERMISSION));
        UsbManager manager = (UsbManager) mContext.getSystemService(Context.USB_SERVICE);
        int number = 0;
        for (UsbDevice usbDevice : manager.getDeviceList().values()) {
            Log.d(TAG, "device:" + usbDevice);
            if (usbDevice.getVendorId() == 9124 && usbDevice.getProductId() == 537) {
                onBackMsg(readerCallback, "请求权限");
                number = 1;
                manager.requestPermission(usbDevice, pendingIntent);
                break;
            }
        }
        if (number == 0) {
            onBackMsg(readerCallback, "没有找到明泰设备");
        }
    }

    private void onBackMsg(IReaderCallback readerCallback, String msg) {
        mReaderBean.setMessage(msg);
        if (readerCallback != null) {
            readerCallback.onMsg(mReaderBean);
        }
    }

    public int readId(IReaderCallback readerCallback) {
        onBackMsg(readerCallback, "开始读身份证");
        IOException e;
        byte[] szName = new byte[128];
        byte[] szSex = new byte[128];
        byte[] szNation = new byte[128];
        byte[] szBirth = new byte[128];
        byte[] szAddress = new byte[128];
        byte[] szIDNo = new byte[36];
        byte[] szDepartment = new byte[128];
        byte[] szDateStart = new byte[128];
        byte[] szDateEnd = new byte[128];
        byte[] szExpir = new byte[128];
        byte[] message = new byte[128];
        int[] nNamelen = new int[2];
        int[] nSexlen = new int[2];
        int[] nNationlen = new int[2];
        int[] nBirthlen = new int[2];
        int[] nAddresslen = new int[2];
        int[] nIDNolen = new int[2];
        int[] nDepartmentlen = new int[2];
        int[] nDateStartlen = new int[2];
        int[] nDateEndlen = new int[2];
        int[] nExpirlen = new int[2];
        int res = mt3yApi.mt8IDCardRead(szName, szSex, szNation, szBirth, szAddress, szIDNo, szDepartment, szDateStart, szDateEnd, new int[8], new byte[3072]);

        if (res != 0) {
            onBackMsg(readerCallback, "身份证读卡失败-100");
//            close();
//            init(null);
            return -100;
        }
        Log.d("读卡code",res+"测试");

        try {
            String StrAddress2;
            String name = new String(szName, "UTF-16LE");
            mReaderBean.setName(name.trim());
            //            try {
////                mTextView.append(str.trim() + "\n");
////                mTextView.append(getsexinfo(szSex).trim() + "\n");
//                str = new String(szBirth, "UTF-16LE");
//
//            } catch (IOException e2) {
//                e = e2;
//                StrName = str;
//                e.printStackTrace();
//                Log.d(TAG, "照片解码异常");
//                return -200;
//            }
            try {
//                mTextView.append(getnation(szNation) + "\n");
//                mTextView.append(str.trim() + "\n");
//                mTextView.append(StrAddress.trim() + "\n");
                StrAddress2 = new String(szAddress, "UTF-16LE");
                try {
//                    mTextView.append(StrAddress2.trim() + "\n");
                    String idNum = new String(szIDNo, "UTF-16LE");
                    mReaderBean.setIdNum(idNum.trim());
                    if (readerCallback != null) {
                        readerCallback.onMsg(mReaderBean);
                    }
                } catch (IOException e3) {
                    e = e3;
//                    StrBirth = str;
//                    StrName = str;
                    e.printStackTrace();
                    Log.d(TAG, "照片解码异常");
                    return -200;
                }
            } catch (IOException e4) {
                e = e4;
//                StrBirth = str;
//                StrName = str;
                e.printStackTrace();
                Log.d(TAG, "照片解码异常");
                return -200;
            }
//            try {
//                mTextView.append(str.trim() + "\n");
//                str = new String(szDepartment, "UTF-16LE");
//                try {
//                    mTextView.append(str.trim() + "\n");
//                    str = new String(szDateStart, "UTF-16LE");
//                } catch (IOException e5) {
//                    e = e5;
//                    StrIDNo = str;
//                    StrDepartment = str;
//                    StrAddress = StrAddress2;
//                    StrBirth = str;
//                    StrName = str;
//                    e.printStackTrace();
//                    Log.d(TAG, "照片解码异常");
//                    return -200;
//                }
//                try {
//                    mTextView.append(str.trim() + "\n");
//                    str = new String(szDateEnd, "UTF-16LE");
//                    mTextView.append(str.trim() + "\n");
//                    StrIDNo = str;
//                    StrDateEnd = str;
//                    StrDateStart = str;
//                    StrDepartment = str;
//                    StrAddress = StrAddress2;
//                    StrBirth = str;
//                    StrName = str;
//                    return 0;
//                } catch (IOException e7) {
//                    e = e7;
//                    StrIDNo = str;
//                    StrDateStart = str;
//                    StrDepartment = str;
//                    StrAddress = StrAddress2;
//                    StrBirth = str;
//                    StrName = str;
//                    e.printStackTrace();
//                    Log.d(TAG, "照片解码异常");
//                    return -200;
//                }
//            } catch (IOException e8) {
//                e = e8;
//                StrIDNo = str;
//                StrAddress = StrAddress2;
//                StrBirth = str;
//                StrName = str;
//                e.printStackTrace();
//                Log.d(TAG, "照片解码异常");
//                return -200;
//            }
            return 0;
        } catch (IOException e9) {
            e = e9;
            e.printStackTrace();
            Log.d(TAG, "照片解码异常");
            return -200;
        }
    }


    @Override
    public void close() {
        if (mReaderReicever != null) {
            mContext.unregisterReceiver(mReaderReicever);
            mReaderReicever = null;
        }
        if (mt3yApi.mt8deviceclose() == 0) {
            Log.d(TAG, "关闭设备成功");
        } else {
            Log.d(TAG, "关闭设备失败");
        }
    }

}
